﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Windows.Data;

namespace Triples {
    public class PlayableCardControl : ContentControl {
        public event PropertyChangedEventHandler PropertyChanged;
        private BoardControl _Board;


        //public static readonly DependencyProperty ArrangeProgressProperty =
        //    DependencyProperty.Register("ArrangeProgress", typeof(double),
        //    typeof(PlayableCardControl), new PropertyMetadata(null));


        public double ArrangeProgress {
            get { return AnimatablePanel.GetArrangeProgress (this); }
            set { AnimatablePanel.SetArrangeProgress(this, value); }
        }

        
        private BoardControl Board {
            get { return _Board ?? (_Board = Utilities.FindAncestor<BoardControl>(this) as BoardControl); }
        }
        

        public PlayableCardControl () {
            DefaultStyleKey = typeof(PlayableCardControl);
        }


        private void Set<T1>(ref bool var, bool value, string p) {
            var = value;
            if (null != PropertyChanged) PropertyChanged(this, new PropertyChangedEventArgs(p));
        }


        protected override void OnTap(GestureEventArgs e) {
            base.OnTap(e);

            if (null != Board) {
                IPlayableCard card = this.DataContext as IPlayableCard;

                if (null != card) {
                    if (!card.IsSelected) Board.Controller.SelectCard(card);
                    else Board.Controller.DeselectCard(card);
                }
            }
        }


        protected override Size MeasureOverride(Size availableSize) {
            Size ret = base.MeasureOverride(availableSize);
            return ret;
        }


        protected override Size ArrangeOverride(Size finalSize) {
            Size ret = base.ArrangeOverride(finalSize);
            return ret;
        }
    }
}
